#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <linux/un.h>
#include <sys/socket.h>

#define SOCK_IPC_FILE "/home/hcgy/Documents/esm6800h/test/sock_ipc.txt"

int main(int argc, char **argv)
{

    int                 fd;
    struct sockaddr_un  serveraddr;
 
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        fprintf(stderr, "socket: %s\n", strerror(errno));
        return -1;
    }
 
    serveraddr.sun_family = AF_UNIX;
    snprintf(serveraddr.sun_path, sizeof(serveraddr.sun_path), "%s", SOCK_IPC_FILE);
 
    // if (bind(fd, (struct sockaddr *)&localaddr, sizeof(struct sockaddr_un)) < 0) {
    //     fprintf(stderr, "bind: %s\n", strerror(errno));
    //     return -1;
    // }
    int result = connect(fd, (struct sockaddr *)&serveraddr, (socklen_t)sizeof(struct sockaddr_un));
    if (result)
    {
        perror("ops: client\n");
        exit(1);
    }

    char buff[128] = {0};
    sprintf(buff,"%s","test sock ipc\n");

    write(fd, buff, strlen(buff));

    memset(buff,0,128);

    int readRet = read(fd, buff, 128);

    if(readRet>0)
    {
        printf("recv %d bytes,%s",readRet,buff);
    }


 
    return fd;
}
